package com.party.web.biz.sign;

import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.party.common.paging.Page;
import com.party.common.utils.StringUtils;
import com.party.core.model.sign.*;
import com.party.core.service.cos.CosBizService;
import com.party.core.service.sign.ISignApplyService;
import com.party.core.service.sign.ISignGroupService;
import com.party.core.service.sign.ISignProjectService;
import com.party.core.service.sign.ISignRecordService;
import com.party.core.service.sign.biz.SignBusinessBizService;
import com.party.web.biz.file.FileBizService;
import com.party.web.biz.openPlatform.NotifyBizService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 签到报名业务接口
 * Created by wei.li
 *
 * @date 2017/6/12 0012
 * @time 18:19
 */

@Service
public class SignApplyBizService {

    @Autowired
    private ISignApplyService signApplyService;

    @Autowired
    private ISignProjectService signProjectService;

    @Autowired
    private ISignGroupService signGroupService;

    @Autowired
    private ISignRecordService signRecordService;

    @Autowired
    private SignBusinessBizService signBusinessBizService;

    @Autowired
    private FileBizService fileBizService;

    @Autowired
    private NotifyBizService notifyBizService;

    @Autowired
    private CosBizService cosBizService;

    /**
     * 审核
     * @param status 状态
     * @param id 报名编号
     */
    @Transactional
    public void check(Integer status, String id){
        SignApply signApply = signApplyService.get(id);
        signApply.setStatus(status);
        signApplyService.update(signApply);

        //项目数据调整
        SignProject signProject = signProjectService.get(signApply.getProjectId());
        signProject.setNotAuditNum(signProject.getNotAuditNum() - 1);

        if (SignApplyStatus.PASS_STATUS.getCode().equals(status)){
            signProject.setPassNum(signProject.getPassNum() + 1);
        }
        else if (SignApplyStatus.REFUSE_STATUS.getCode().equals(status)){
            signProject.setRefuseNum(signProject.getRefuseNum() + 1);
        }
        signProjectService.update(signProject);

        //小组数据调整
        if (!Strings.isNullOrEmpty(signApply.getGroupId())){
            SignGroup signGroup = signGroupService.get(signApply.getGroupId());
            signGroup.setNotAuditNum(signGroup.getNotAuditNum() - 1);
            if (SignApplyStatus.PASS_STATUS.getCode().equals(status)){
                signGroup.setPassNum(signGroup.getPassNum() + 1);
                signGroup.setMemberNum(signGroup.getMemberNum() + 1);
            }
            else if (SignApplyStatus.REFUSE_STATUS.getCode().equals(status)){
                signGroup.setRefuseNum(signGroup.getRefuseNum() + 1);
            }
            signGroupService.update(signGroup);
        }


    }


    /**
     * 删除报名信息
     *
     * @param id 报名编号
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void delete(String id) {
        SignApply signApply = signApplyService.get(id);
        SignProject signProject = signProjectService.get(signApply.getProjectId());
        signProject.setApplyNum(signProject.getApplyNum() - 1);
        if (SignApplyStatus.PASS_STATUS.getCode().equals(signApply.getStatus()) && signProject.getPassNum() > 0) {
            signProject.setPassNum(signProject.getPassNum() - 1);
        } else if (SignApplyStatus.REFUSE_STATUS.getCode().equals(signApply.getStatus())) {
            signProject.setRefuseNum(signProject.getRefuseNum() - 1);
        }
        signProjectService.update(signProject);

        if (!Strings.isNullOrEmpty(signApply.getGroupId())) {
            SignGroup signGroup = signGroupService.get(signApply.getGroupId());
            signGroup.setApplyNum(signGroup.getApplyNum() - 1);
            if (SignApplyStatus.PASS_STATUS.getCode().equals(signApply.getStatus()) && signGroup.getPassNum() > 0) {
                signGroup.setPassNum(signGroup.getPassNum() - 1);
                if (SignGradeStatus.EFFECTIVE.getCode().equals(signApply.getGradeStatus())) {
                    signGroup.setMemberNum(signGroup.getMemberNum() - 1);
                }
            } else if (SignApplyStatus.REFUSE_STATUS.getCode().equals(signApply.getStatus())) {
                signGroup.setRefuseNum(signGroup.getRefuseNum() - 1);
            }

            // 步数计算
            if (SignGradeStatus.EFFECTIVE.getCode().equals(signApply.getGradeStatus())) {
                signGroup.setStepNum(signGroup.getStepNum() - signApply.getStepNum());
            }
            signGroupService.update(signGroup);
        }

        //删除签到记录
        signRecordService.deleteByApplyId(id);
        signApplyService.delete(id);
    }

    /**
     * 移动小组
     * @param id 编号
     * @param groupId 小组号
     */
    @Transactional
    public void changeGroup(String id, String groupId){
        //改变小组数据
        SignApply dbApply = signApplyService.get(id);
        if (!Strings.isNullOrEmpty(dbApply.getGroupId())){
            SignGroup signGroup = signGroupService.get(dbApply.getGroupId());
            signGroup.setApplyNum(signGroup.getApplyNum() - 1);
            signGroup.setMemberNum(signGroup.getMemberNum() - 1);
            signGroup.setStepNum(signGroup.getStepNum() - dbApply.getStepNum());
            if (SignApplyStatus.PASS_STATUS.getCode().equals(dbApply.getStatus())){
                signGroup.setPassNum(signGroup.getPassNum() - 1);
            }
            else if (SignApplyStatus.REFUSE_STATUS.getCode().equals(dbApply.getStatus())){
                signGroup.setRefuseNum(signGroup.getRefuseNum() - 1);
            }
            else if (SignApplyStatus.NOT_AUDIT_STATUS.getCode().equals(dbApply.getStatus())){
                signGroup.setNotAuditNum(signGroup.getNotAuditNum() - 1);
            }
            signGroupService.update(signGroup);
        }

        if (!Strings.isNullOrEmpty(groupId)){
            SignGroup signGroup = signGroupService.get(groupId);
            signGroup.setApplyNum(signGroup.getApplyNum() + 1);
            signGroup.setStepNum(signGroup.getStepNum() + dbApply.getStepNum());
            signGroup.setMemberNum(signGroup.getMemberNum() + 1);
            if (SignApplyStatus.PASS_STATUS.getCode().equals(dbApply.getStatus())){
                signGroup.setPassNum(signGroup.getPassNum() + 1);
            }
            else if (SignApplyStatus.REFUSE_STATUS.getCode().equals(dbApply.getStatus())){
                signGroup.setRefuseNum(signGroup.getRefuseNum() + 1);
            }
            else if (SignApplyStatus.NOT_AUDIT_STATUS.getCode().equals(dbApply.getStatus())){
                signGroup.setNotAuditNum(signGroup.getNotAuditNum() + 1);
            }
            signGroupService.update(signGroup);
        }

        dbApply.setGroupId(groupId);
        signApplyService.update(dbApply);
    }

    /**
     * 小组人员排行
     *
     * @param groupMember 查询条件
     * @param page        分页参数
     * @return
     */
    public List<GroupMember> groupMemberList(GroupMember groupMember, Integer type, Page page) {
        Map<String, Object> params = signBusinessBizService.appendSearchParams(type);
        params.put("memberName", groupMember.getMemberName());
        List<GroupMember> list = signApplyService.groupMemberListXcx(groupMember, params, page);
        return list;
    }

    /**
     * 人员排行
     *
     * @param groupMember 项目编号
     * @param type      类型
     * @param page      分页参数
     * @return 排名列表
     */
    public List<GroupMember> rankList(GroupMember groupMember, Integer type, Page page) {
        Map<String, Object> params = signBusinessBizService.appendSearchParams(type);
        params.put("memberName", groupMember.getMemberName());
        params.put("groupId", groupMember.getGroupId());
        List<GroupMember> list = signApplyService.rankListXcx(groupMember.getProjectId(), params, page);
        return list;
    }

    /**
     * 获取我的打卡
     *
     * @param memberId 用户id
     * @return
     */
    public Map<String, String> getReturnUrl(String memberId) {
        Map<String, String> map = Maps.newHashMap();
        if (StringUtils.isNotEmpty(memberId)) {
            SignProject signProject = new SignProject();
            signProject.setCreateBy(memberId);
            List<SignProjectAuthor> projectIds = signProjectService.myJoinList(signProject, null);
            if (projectIds.size() == 1) {
                map.put("type", "image");
                map.put("value", projectIds.get(0).getId());
                return map;
            }
        }
        map.put("type", "url");
        map.put("value", "sign/my_list.html");
        return map;
    }

    /**
     * 上传临时素材
     *
     * @param projectId 项目id
     * @return 素材id
     * @throws Exception
     */
    public String uploadTempMedia(String projectId) throws Exception {
        SignProject signProject = signProjectService.get(projectId);
        String filePath = fileBizService.getUploadCIPath(signProject.getCreateBy(), "signProject");
        String url = cosBizService.getPreviewPictureUrl(filePath + signProject.getId());
        return notifyBizService.uploadTempMedia(url, signProject.getCreateBy());
    }
}
